package com.example.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.entity.PsLineInfo;
import com.example.entity.PsPoleInfo;
import com.example.exception.CustomException;
import com.example.mapper.PsPoleInfoMapper;
import com.example.service.PsPoleInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.vo.PsPoleInfoVo;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author yt
 * @since 2024-04-23
 */
@Service
@Slf4j
@RequiredArgsConstructor
public class PsPoleInfoServiceImpl extends ServiceImpl<PsPoleInfoMapper, PsPoleInfo> implements PsPoleInfoService {

    private final PsPoleInfoMapper psPoleInfoMapper;
    /**
     * 查询
     *
     * @param page         Mybatis-Plus所提供的分页对象,必须位于第一个参数的位置
     * @param psPoleInfoVo
     * @return
     */
    @Override
    public IPage<PsPoleInfo> findBySearch(Page<PsPoleInfo> page, PsPoleInfoVo psPoleInfoVo) {
        Page<PsPoleInfo> page1 = new Page<>(psPoleInfoVo.getPageNum(), psPoleInfoVo.getPageSize());
        return psPoleInfoMapper.findBySearch(page1, psPoleInfoVo);
    }

    /**
     * 修改塔杆信息
     *
     * @param psPoleInfo
     */
    @Override
    public void updatePsPoleInfo(PsPoleInfo psPoleInfo) {
        psPoleInfoMapper.updateById(psPoleInfo);
    }

    /**
     * 添加塔杆信息
     *
     * @param psPoleInfo
     */
    @Override
    public void add(PsPoleInfo psPoleInfo) {
        psPoleInfoMapper.insert(psPoleInfo);
    }

    /**
     * 选择目标数据，点击删除，若成功，提示删除成功；若不成功，提示失败原因。
     * 约束：所属线路不为空，不能被删除，只能停用；所属线路为空，可以被删除。
     *
     * @param id
     */
    @Override
    public int delete(Integer id) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("id", id);
        PsPoleInfo psPoleInfo = psPoleInfoMapper.selectOne(queryWrapper);
        log.info("线路名称:{}", psPoleInfo.getLineId());
        if (psPoleInfo.getLineId() == null) {
            psPoleInfoMapper.deleteById(id);
        } else {
            throw new CustomException("不能删除");
        }
        return 1;
    }

    /**
     * 从最低到最高
     * @return
     */
    @Override
    public List<PsPoleInfo> selectAllpoleCodeAsc() {
        List<PsPoleInfo> psPoleInfos = psPoleInfoMapper.selectAllpoleCodeAsc();
        return psPoleInfos;
    }

    @Override
    public List<PsPoleInfo> selectAllPoleCodeDesc() {
        List<PsPoleInfo> psPoleInfos = psPoleInfoMapper.selectAllPoleCodeDesc();
        return psPoleInfos;
    }

    @Override
    public int updateIsStart(PsPoleInfo psPoleInfo) {
        int i = 0;
        if (psPoleInfo.getIsstart() == 0) {
            UpdateWrapper<PsPoleInfo> updateWrapper = new UpdateWrapper<>();
            updateWrapper.set("isStart", 1).eq("id", psPoleInfo.getId());
            i = psPoleInfoMapper.update(psPoleInfo, updateWrapper);
        } else if (psPoleInfo.getIsstart() == 1) {
            UpdateWrapper<PsPoleInfo> updateWrapper = new UpdateWrapper<>();
            updateWrapper.set("isStart", 0).eq("id", psPoleInfo.getId());
            i = psPoleInfoMapper.update(psPoleInfo, updateWrapper);
        }
        return i;
    }
}
